# Simple MCP Test Script
param(
    [string]$BaseUrl = "http://localhost:5002"
)

Write-Host "=== MCP Weather Alert System Test ===" -ForegroundColor Green
Write-Host "Service URL: $BaseUrl" -ForegroundColor Yellow
Write-Host ""

# Test health check
Write-Host "1. Health Check" -ForegroundColor Cyan
try {
    $health = Invoke-RestMethod -Uri "$BaseUrl/api/mcp/health" -Method GET
    Write-Host "Health Status: $($health.status)" -ForegroundColor Green
    Write-Host "Service: $($health.service)" -ForegroundColor Green
}
catch {
    Write-Host "Health check failed: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host ""

# Test service info
Write-Host "2. Service Info" -ForegroundColor Cyan
try {
    $info = Invoke-RestMethod -Uri "$BaseUrl/api/mcp/info" -Method GET
    Write-Host "Server: $($info.serverInfo.name)" -ForegroundColor Green
    Write-Host "Version: $($info.serverInfo.version)" -ForegroundColor Green
    Write-Host "Tool Count: $($info.statistics.toolCount)" -ForegroundColor Green
}
catch {
    Write-Host "Service info failed: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host ""

# Test MCP Initialize
Write-Host "3. MCP Initialize" -ForegroundColor Cyan
try {
    $initRequest = @{
        jsonrpc = "2.0"
        id = 1
        method = "initialize"
        params = @{
            protocolVersion = "2024-11-05"
            capabilities = @{
                tools = @{
                    listChanged = $false
                }
            }
            clientInfo = @{
                name = "Test Client"
                version = "1.0.0"
            }
        }
    } | ConvertTo-Json -Depth 10

    $response = Invoke-RestMethod -Uri "$BaseUrl/api/mcp" -Method POST -Body $initRequest -ContentType "application/json"
    Write-Host "Initialize Success: $($response.result.serverInfo.name)" -ForegroundColor Green
}
catch {
    Write-Host "Initialize failed: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host ""

# Test Tools List
Write-Host "4. Tools List" -ForegroundColor Cyan
try {
    $toolsRequest = @{
        jsonrpc = "2.0"
        id = 2
        method = "tools/list"
        params = @{}
    } | ConvertTo-Json -Depth 10

    $response = Invoke-RestMethod -Uri "$BaseUrl/api/mcp" -Method POST -Body $toolsRequest -ContentType "application/json"
    Write-Host "Available Tools: $($response.result.tools.Count)" -ForegroundColor Green
    foreach ($tool in $response.result.tools) {
        Write-Host "  - $($tool.name): $($tool.description)" -ForegroundColor White
    }
}
catch {
    Write-Host "Tools list failed: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host ""

# Test Weather Tool
Write-Host "5. Test Weather Tool" -ForegroundColor Cyan
try {
    $weatherRequest = @{
        jsonrpc = "2.0"
        id = 3
        method = "tools/call"
        params = @{
            name = "get_current_weather"
            arguments = @{
                city = "Beijing"
                countryCode = "CN"
            }
        }
    } | ConvertTo-Json -Depth 10

    $response = Invoke-RestMethod -Uri "$BaseUrl/api/mcp" -Method POST -Body $weatherRequest -ContentType "application/json"
    if ($response.result.content) {
        Write-Host "Weather Tool Success" -ForegroundColor Green
        Write-Host "Response: $($response.result.content[0].text)" -ForegroundColor White
    }
}
catch {
    Write-Host "Weather tool failed: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host ""

# Test MES Tool
Write-Host "6. Test MES Tool" -ForegroundColor Cyan
try {
    $mesRequest = @{
        jsonrpc = "2.0"
        id = 4
        method = "tools/call"
        params = @{
            name = "query_production_orders"
            arguments = @{
                status = "进行中"
                pageSize = "5"
            }
        }
    } | ConvertTo-Json -Depth 10

    $response = Invoke-RestMethod -Uri "$BaseUrl/api/mcp" -Method POST -Body $mesRequest -ContentType "application/json"
    if ($response.result.content) {
        Write-Host "MES Tool Success" -ForegroundColor Green
        Write-Host "Response: $($response.result.content[0].text)" -ForegroundColor White
    }
}
catch {
    Write-Host "MES tool failed: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host ""
Write-Host "=== Test Complete ===" -ForegroundColor Green
Write-Host "MCP Server is ready for Coze AI integration!" -ForegroundColor Cyan
Write-Host "Integration URL: $BaseUrl/api/mcp" -ForegroundColor Yellow
